﻿using HuaHan.WorkTicket.Domain.Repositories.EntityFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HuaHan.WorkTicket.Domain.Repositories
{
    public class UnitOfWork : IDisposable
    {
        private WorkTicketDBContext context = new WorkTicketDBContext();
        private GenericRepository<Department> departmentRepository;
        private GenericRepository<Color> colorRepository;
        private GenericRepository<Menu> menuRepository;
        private GenericRepository<Group> groupRepository;
        private GenericRepository<Customer> customerRepository;
        private GenericRepository<StyleCategory> styleCategoryRepository;
        private GenericRepository<Style> styleRepository;
        private GenericRepository<Size> sizeRepository;
        private GenericRepository<Unit> unitRepository;
        private GenericRepository<ColorGroup> colorGroupRepository;
        private GenericRepository<ColorGroupColor> colorGroupColorRepository;
        private GenericRepository<Employee> employeeRepository;

        private GenericRepository<SizeGroup> sizeGroupRepository;
        private GenericRepository<SizeGroupSize> sizeGroupSizeRepository;


        public GenericRepository<Department> DepartmentRepository
        {
            get
            {

                if (this.departmentRepository == null)
                {
                    this.departmentRepository = new GenericRepository<Department>(context);
                }
                return departmentRepository;
            }
        }

        public GenericRepository<Color> ColorRepository
        {
            get
            {

                if (this.colorRepository == null)
                {
                    this.colorRepository = new GenericRepository<Color>(context);
                }
                return colorRepository;
            }
        }

        public GenericRepository<Menu> MenuRepository
        {
            get
            {

                if (this.menuRepository == null)
                {
                    this.menuRepository = new GenericRepository<Menu>(context);
                }
                return menuRepository;
            }
        }

        public GenericRepository<Group> GroupRepository
        {
            get
            {

                if (this.groupRepository == null)
                {
                    this.groupRepository = new GenericRepository<Group>(context);
                }
                return groupRepository;
            }
        }

        public GenericRepository<Customer> CustomerRepository
        {
            get
            {

                if (this.customerRepository == null)
                {
                    this.customerRepository = new GenericRepository<Customer>(context);
                }
                return customerRepository;
            }
        }

        public GenericRepository<StyleCategory> StyleCategoryRepository
        {
            get
            {

                if (this.styleCategoryRepository == null)
                {
                    this.styleCategoryRepository = new GenericRepository<StyleCategory>(context);
                }
                return styleCategoryRepository;
            }
        }

        public GenericRepository<Style> StyleRepository
        {
            get
            {

                if (this.styleRepository == null)
                {
                    this.styleRepository = new GenericRepository<Style>(context);
                }
                return styleRepository;
            }
        }

        public GenericRepository<Size> SizeRepository
        {
            get
            {

                if (this.sizeRepository == null)
                {
                    this.sizeRepository = new GenericRepository<Size>(context);
                }
                return sizeRepository;
            }
        }

        public GenericRepository<Unit> UnitRepository
        {
            get
            {

                if (this.unitRepository == null)
                {
                    this.unitRepository = new GenericRepository<Unit>(context);
                }
                return unitRepository;
            }
        }

        public GenericRepository<ColorGroup> ColorGroupRepository
        {
            get
            {

                if (this.colorGroupRepository == null)
                {
                    this.colorGroupRepository = new GenericRepository<ColorGroup>(context);
                }
                return colorGroupRepository;
            }
        }

        public GenericRepository<ColorGroupColor> ColorGroupColorRepository
        {
            get
            {

                if (this.colorGroupColorRepository == null)
                {
                    this.colorGroupColorRepository = new GenericRepository<ColorGroupColor>(context);
                }
                return colorGroupColorRepository;
            }
        }


        public GenericRepository<SizeGroup> SizeGroupRepository
        {
            get
            {

                if (this.sizeGroupRepository == null)
                {
                    this.sizeGroupRepository = new GenericRepository<SizeGroup>(context);
                }
                return this.sizeGroupRepository;
            }
        }

        public GenericRepository<SizeGroupSize> SizeGroupSizeRepository
        {
            get
            {

                if (this.sizeGroupSizeRepository == null)
                {
                    this.sizeGroupSizeRepository = new GenericRepository<SizeGroupSize>(context);
                }
                return sizeGroupSizeRepository;
            }
        }

        public GenericRepository<Employee> EmployeeRepository
        {
            get
            {

                if (this.employeeRepository == null)
                {
                    this.employeeRepository = new GenericRepository<Employee>(context);
                }
                return this.employeeRepository;
            }
        }


        public void Commit()
        {
            context.SaveChanges();
        }

        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    context.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}
